define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'scanwork/employee/index' + location.search,
                    add_url: 'scanwork/employee/add',
                    edit_url: 'scanwork/employee/edit',
                    del_url: 'scanwork/employee/del',
                    multi_url: 'scanwork/employee/multi',
                    table: 'employee',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'employee_code', title: __('员工编码'), operate: 'LIKE'},
                        {field: 'employee_name', title: __('员工姓名'), operate: 'LIKE'},
                        {field: 'position', title: __('岗位'), operate: 'LIKE'},
                        {field: 'skill_level', title: __('技能等级'), searchList: {"basic":__('基础'),"intermediate":__('中级'),"advanced":__('高级')}, formatter: function(value, row, index) {
                            var levelMap = {
                                'basic': '基础',
                                'intermediate': '中级', 
                                'advanced': '高级'
                            };
                            return levelMap[value] || value;
                        }},
                        {field: 'operable_processes', title: __('可操作工序'), formatter: function(value, row, index) {
                            if (value && Array.isArray(value)) {
                                return value.join(', ');
                            }
                            return value || '';
                        }},
                        {field: 'contact_phone', title: __('联系电话'), operate: 'LIKE'},
                        {field: 'hourly_rate', title: __('工时单价'), formatter: function(value, row, index) {
                            return value ? value + '元/小时' : '0元/小时';
                        }},
                        {field: 'status', title: __('状态'), searchList: {"1":__('正常'),"0":__('隐藏')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: function(value, row, index) {
                            var table = this.table;
                            var options = table ? table.bootstrapTable('getOptions') : {};
                            var buttons = [];
                            buttons.push({
                                name: 'workload',
                                text: __('工作负荷'),
                                title: __('查看工作负荷'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-chart-bar',
                                url: 'scanwork/employee/workload/ids/{ids}',
                                callback: function (data) {
                                    table.bootstrapTable('refresh');
                                }
                            });
                            return Table.api.formatter.operate.call(this, value, row, index, buttons);
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        workload: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
